import allure
from selenium.webdriver.common.by import By

from page.home_page import HomePage


class ChannelTempPage(HomePage):
    _ADD = (By.XPATH, "//*[text()='添加']")
    _ORDER = (By.XPATH, '//*[text()="订单编号"]')
    _SOURCE = (By.XPATH, '//*[text()="来源单号"]')
    _GOODS = (By.XPATH, '//*[text()="商品名称"]')
    _NUM = (By.XPATH, '//*[text()="数量"]')
    _COMMIT = (By.XPATH, '//*[text()="提交"]')
    _INPUT_BOX = (By.XPATH, '//*[@class="inputWidth el-input"]//input')
    _SUCCESS = (By.XPATH, '//*[text()="操作成功"]')

    @allure.step('添加渠道模板，选择字段')
    def click_add(self, name):
        with allure.step("点击添加"):
            self.find_and_click(self._ADD)
        with allure.step("输入模板名称"):
            self.find_and_send(name, self._INPUT_BOX)
        with allure.step("选择模板字段"):
            self.find_and_click(self._ORDER)
            self.find_and_click(self._SOURCE)
            self.find_and_click(self._GOODS)
            self.find_and_click(self._NUM)
        with allure.step("点击提交"):
            self.find_and_click(self._COMMIT)
            self.set_time(1)
        return self.get_text(self._SUCCESS)

    _TEMP_INPUT = (By.XPATH, '//*[@placeholder="模板名称"]')
    _SEARCH = (By.XPATH, '//*[text()="查询"]')
    _RESULT = (By.CSS_SELECTOR, '.el-table__row')

    @allure.step('搜索渠道模板')
    def search(self, name):
        self.set_time(1)
        with allure.step("输入渠道名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
            self.set_time(1)

        return len(self.finds(self._RESULT))

    _NO_DATA = (By.XPATH, '//*[text()="暂无数据"]')

    @allure.step('搜索渠道模板')
    def search_fail(self, name):
        self.set_time(1)
        with allure.step("输入渠道名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)

        return self.get_text(self._NO_DATA)

    _FIX = (By.XPATH, "//*[text()='修改']")
    _DELETE = (By.XPATH, "//*[text()='删除']")

    @allure.step('修改渠道模板')
    def fix(self, name):
        with allure.step("输入渠道名称"):
            self.find_and_send(name, self._TEMP_INPUT)
        with allure.step("点击查询"):
            self.find_and_click(self._SEARCH)
        with allure.step("点击修改"):
            self.find_and_click(self._FIX)
        with allure.step("点击删除"):
            self.find_and_click(self._DELETE)

        with allure.step("点击提交"):
            self.find_and_click(self._COMMIT)
            self.set_time(1)
        return self.get_text(self._SUCCESS)



